package com.cskaoyan.service.admin.configuration;

import com.cskaoyan.bean.MarketSystem;
import com.cskaoyan.bean.MarketSystemExample;
import com.cskaoyan.bean.vo.admin.configuration.AdminConfigMallVO;
import com.cskaoyan.bean.vo.admin.configuration.AdminConfigWXVO;
import com.cskaoyan.mapper.MarketSystemMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*
 * @Auther:王贲
 * @Date:2022/9/8
 * @Description:
 * @VERSON:1.8
 */
@Service
@Transactional
public class AdminConfigServiceImpl implements AdminConfigService {

    @Autowired
    MarketSystemMapper marketSystemMapper;

    @SneakyThrows
    @Override
    public AdminConfigMallVO selectConfigInfo() {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();

        AdminConfigMallVO adminConfigMallVO = new AdminConfigMallVO();

        //通过反射拿到类的字节码文件
        Class<?> aClass = Class.forName("com.cskaoyan.bean.vo.admin.configuration.AdminConfigMallVO");
        //通过字节码文件拿到类的成员变量
        Field[] fields = aClass.getDeclaredFields();
        //模糊查询 ，查询出数据库中的key
        criteria.andKeyNameLike("%market_mall_%");
        List<MarketSystem> list = marketSystemMapper.selectByExample(marketSystemExample);
        //反射赋值
        //遍历
        for (MarketSystem marketSystem : list) {
            for (Field field : fields) {
                //判断如果成员变量的名字等于类中的变量名
                if (field.getName().equals(marketSystem.getKeyName())) {
                    //强制进入
                    field.setAccessible(true);
                    //将value值赋给类
                    field.set(adminConfigMallVO,marketSystem.getKeyValue());
                }
            }
        }
        //返回类
        return adminConfigMallVO;
    }

    @Override
    @SneakyThrows
    public AdminConfigMallVO selectExpressConfigInfo() throws IllegalAccessException {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();

        AdminConfigMallVO adminConfigMallVO = new AdminConfigMallVO();

        //通过反射拿到类的字节码文件
        Class<?> aClass = Class.forName("com.cskaoyan.bean.vo.admin.configuration.AdminConfigMallVO");
        //通过字节码文件拿到类的成员变量
        Field[] fields = aClass.getDeclaredFields();
        //模糊查询 ，查询出数据库中的key
        criteria.andKeyNameLike("%market_express_freight_%");
        List<MarketSystem> list = marketSystemMapper.selectByExample(marketSystemExample);
        //反射赋值
        //遍历
        for (MarketSystem marketSystem : list) {
            for (Field field : fields) {
                //判断如果成员变量的名字等于类中的变量名
                if (field.getName().equals(marketSystem.getKeyName())) {
                    //强制进入
                    field.setAccessible(true);
                    //将value值赋给类
                    field.set(adminConfigMallVO,marketSystem.getKeyValue());
                }
            }
        }
        //返回类
        return adminConfigMallVO;
    }

    @Override
    @SneakyThrows
    public AdminConfigMallVO selectOrderConfigInfo() {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();

        AdminConfigMallVO adminConfigMallVO = new AdminConfigMallVO();

        //通过反射拿到类的字节码文件
        Class<?> aClass = Class.forName("com.cskaoyan.bean.vo.admin.configuration.AdminConfigMallVO");
        //通过字节码文件拿到类的成员变量
        Field[] fields = aClass.getDeclaredFields();
        //模糊查询 ，查询出数据库中的key
        criteria.andKeyNameLike("%market_order_%");
        List<MarketSystem> list = marketSystemMapper.selectByExample(marketSystemExample);
        //反射赋值
        //遍历
        for (MarketSystem marketSystem : list) {
            for (Field field : fields) {
                //判断如果成员变量的名字等于类中的变量名
                if (field.getName().equals(marketSystem.getKeyName())) {
                    //强制进入
                    field.setAccessible(true);
                    //将value值赋给类
                    field.set(adminConfigMallVO,marketSystem.getKeyValue());
                }
            }
        }
        //返回类
        return adminConfigMallVO;
    }



    @Override
    @SneakyThrows
    public AdminConfigWXVO selectWxConfigInfo() {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();

        AdminConfigWXVO adminConfigWXVO = new AdminConfigWXVO();

        //通过反射拿到类的字节码文件
        Class<?> aClass = Class.forName("com.cskaoyan.bean.vo.admin.configuration.AdminConfigWXVO");
        //通过字节码文件拿到类的成员变量
        Field[] fields = aClass.getDeclaredFields();
        //模糊查询 ，查询出数据库中的key
        criteria.andKeyNameLike("%market_wx_%");
        List<MarketSystem> list = marketSystemMapper.selectByExample(marketSystemExample);
        //反射赋值
        //遍历
        for (MarketSystem marketSystem : list) {
            for (Field field : fields) {
                //判断如果成员变量的名字等于类中的变量名
                if (field.getName().equals(marketSystem.getKeyName())) {
                    //强制进入
                    field.setAccessible(true);
                    //将value值赋给类
                    field.set(adminConfigWXVO,marketSystem.getKeyValue());
                }
            }
        }
        //返回类
        return adminConfigWXVO;
    }

    // @Override
    // @SneakyThrows
    // public void updateInfo(AdminConfigMallVO adminConfigMallVO) {
    //     MarketSystem marketSystem = new MarketSystem();
    //     Class<?> aClass = Class.forName("com.cskaoyan.bean.vo.admin.configuration.AdminConfigMallVO");
    //     Field[] fields = aClass.getDeclaredFields();
    //     for (Field field : fields) {
    //         if (marketSystem.getKeyName().equals(field.getName())) {
    //             marketSystem.setKeyValue();
    //         }
    //     }
    // }


    @Override
    public void updateInfo(Map<String, String> map) {
        //1. 从map中拿出key值
        Set<String> keySet =map.keySet();
        MarketSystem marketSystem = new MarketSystem();
        //遍历所有的key值
        for (String key : keySet) {
            marketSystem.setKeyName(key);
            marketSystem.setKeyValue(map.get(key));
            MarketSystemExample example = new MarketSystemExample();
            MarketSystemExample.Criteria criteria = example.createCriteria();
            criteria.andKeyNameEqualTo(key);
            marketSystemMapper.updateByExampleSelective(marketSystem, example);
        }
    }
}
